package view.partie;

import java.awt.Component;
import java.awt.Container;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

import server.models.partie.IPartie;
import view.carte.CarteView;
import view.console.Console;
import view.equipe.EquipeContainerView;
import view.terrain.PlateauView;
import view.terrain.TerrainView;
import view.utils.ImageUtil;

public class PartieView implements Observer{
	private static final int WIDTH = 1135;
	private static final int HEIGHT = 650;
	private static final String IMAGE_BACKGROUND = "view/ressources/bois.jpg";
	
	private JFrame mainFrame = null;
	private TerrainView terrain = null;
	
	public PartieView(IPartie partie) {
		((Observable)partie).addObserver(this);
		
		this.initialiseAffichagePartie();	
		this.initialiseCarte(partie);
		this.initialiseTerrain(partie);
		this.initialiseConsole(partie);
		this.initialiseEquipeContainer(partie);
		
		this.mainFrame.setVisible(true);
	}
	
	public boolean plateauLancerEtToucher() {
		return this.terrain.tirTouche();
	}
	
	public void initialiseEquipeContainer(IPartie partie) {
        addComponent(this.mainFrame.getContentPane(), new EquipeContainerView(partie), 569, 471 , 549, 299);
	}

	public void initialiseConsole(IPartie partie) {
        JScrollPane scrollBar = new JScrollPane(new Console(partie));
        scrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        //scrollBar.setPreferredSize(new Dimension(548, 199));
        addComponent(this.mainFrame.getContentPane(), scrollBar, 10, 471 , 548, 199);
	}

	public void initialiseTerrain(IPartie partie) {
		this.terrain = new TerrainView(partie.getTerrain());
		
		addComponent(this.mainFrame.getContentPane(), this.terrain, 568, 10 , 549, 451);
	}

	public void initialiseCarte(IPartie partie) {
		CarteView carte = new CarteView(partie.getTerrain().getListePostesDeTir());
		addComponent(this.mainFrame.getContentPane(), carte, 10, 10 , 548, 451);
	}

	public void initialiseAffichagePartie() {
		this.mainFrame = new JFrame("Angry Pidge");
		this.mainFrame.setSize(1127, 710); 
		//this.mainFrame.setLocation(100, 100);
        ImageIcon icon = ImageUtil.getImageIcon(this, PartieView.IMAGE_BACKGROUND);      
        
        this.mainFrame.setContentPane(new ImagePanel(icon.getImage()));
        this.mainFrame.getContentPane().setLayout(null);

        this.mainFrame.setLocationRelativeTo(null);
        
		this.mainFrame.setResizable(false);
		this.mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	/** Add Component Without a Layout Manager (Absolute Positioning) */
	private void addComponent(Container container,Component c,int x,int y,int width,int height)
	{
		c.setBounds(x,y,width,height);
		container.add(c);
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		
	}
}